#include "pch.h"
#include "ResultError.h"
#include <atomic>
#include <mutex>
#include <map>

namespace yzq {
	static std::atomic_int g_errorCode;
	static std::map<int, tstring> g_errorMap;
	static std::mutex g_mutex;

	void AddErrorMsg(const int errorCode, const tchar * msg) {
		std::lock_guard<std::mutex> lock(g_mutex);
		g_errorMap[errorCode] = msg;
	}

	void SetLastError(const int errorCode) {
		g_errorCode = errorCode;
	}

	int GetLastErrorCode() {
		return g_errorCode;
	}

	const tchar * GetLastErrorMsg(int errCode) {
		std::lock_guard<std::mutex> lock(g_mutex);

		auto iter = g_errorMap.find(errCode);
		if (iter != g_errorMap.end()) {
			return iter->second.c_str();
		}
		return nullptr;
	}
}